package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Awards.Applications;
import com.ruoyi.system.domain.Awards.TeacherReviews;
import com.ruoyi.system.mapper.TeacherReviewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ApplicationsMapper;
import com.ruoyi.system.service.IApplicationsService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-15
 */
@Service
public class ApplicationsServiceImpl implements IApplicationsService
{
    @Autowired
    private ApplicationsMapper applicationsMapper;

    @Autowired
    private TeacherReviewsMapper teacherReviewsMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param applicationId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Applications selectApplicationsByApplicationId(Long applicationId)
    {
        return applicationsMapper.selectApplicationsByApplicationId(applicationId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param applications 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Applications> selectApplicationsList(Applications applications)
    {
        return applicationsMapper.selectApplicationsList(applications);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param applications 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertApplications(Applications applications)
    {
        applications.setCreateTime(DateUtils.getNowDate());
        return applicationsMapper.insertApplications(applications);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param applications 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateApplications(Applications applications)
    {
        applications.setUpdateTime(DateUtils.getNowDate());
        return applicationsMapper.updateApplications(applications);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param applicationIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteApplicationsByApplicationIds(Long[] applicationIds)
    {
        return applicationsMapper.deleteApplicationsByApplicationIds(applicationIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param applicationId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteApplicationsByApplicationId(Long applicationId)
    {
        return applicationsMapper.deleteApplicationsByApplicationId(applicationId);
    }

    @Override
    public AjaxResult reviewStart(Applications applications) {
        applications.setApplicationDate(DateUtils.getNowDate());
        if(applications.getApplicationAwardTypeId()==null){
            return  AjaxResult.error("奖金类型id不能为空");
        }
        if(applications.getApplicationId()==null){
            applicationsMapper.insertApplications(applications);
        }else {
            applicationsMapper.updateApplications(applications);
        }
        //新建一条待审批记录
        TeacherReviews teacherReviews=new TeacherReviews();
        teacherReviews.setApplicationId(applications.getApplicationId());
        teacherReviews.setReviewerId(applications.getReviewerId());
        teacherReviews.setReviewer(applications.getReviewer());
        teacherReviews.setReviewerLevel(applications.getReviewerLevel());
        teacherReviews.setReviewResult("待审核");
        teacherReviewsMapper.insertTeacherReviews(teacherReviews);
        return AjaxResult.success();
    }

    @Override
    public List<Map<String, Object>> getClassReviewer(Long deptId, String roleKey) {

        return applicationsMapper.getClassReviewer(deptId==null?null:deptId.toString(),roleKey);
    }
}
